package com.ruoyi.loss.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.loss.domain.WellLostRecord;
import com.ruoyi.loss.domain.bo.WellLostRecordBo;
import com.ruoyi.loss.domain.vo.WellLostRecordVo;
import com.ruoyi.loss.mapper.WellLostRecordMapper;
import com.ruoyi.loss.service.IWellLostRecordService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 井漏记录Service业务层处理
 *
 * @author yangyong
 * @date 2023-12-26
 */
@RequiredArgsConstructor
@Service
public class WellLostRecordServiceImpl implements IWellLostRecordService {

    private final WellLostRecordMapper baseMapper;

    /**
     * 查询井漏记录
     */
    @Override
    public WellLostRecordVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询井漏记录列表
     */
    @Override
    public TableDataInfo<WellLostRecordVo> queryPageList(WellLostRecordBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WellLostRecord> lqw = buildQueryWrapper(bo);
        Page<WellLostRecordVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询井漏记录列表
     */
    @Override
    public List<WellLostRecordVo> queryList(WellLostRecordBo bo) {
        LambdaQueryWrapper<WellLostRecord> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WellLostRecord> buildQueryWrapper(WellLostRecordBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WellLostRecord> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getWellNo()), WellLostRecord::getWellNo, bo.getWellNo());
        lqw.eq(bo.getComplexType() != null, WellLostRecord::getComplexType, bo.getComplexType());
        lqw.between(params.get("beginRecordTime") != null && params.get("endRecordTime") != null,
            WellLostRecord::getRecordTime, params.get("beginRecordTime"), params.get("endRecordTime"));
        return lqw;
    }

    /**
     * 新增井漏记录
     */
    @Override
    public Boolean insertByBo(WellLostRecordBo bo) {
        WellLostRecord add = BeanUtil.toBean(bo, WellLostRecord.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改井漏记录
     */
    @Override
    public Boolean updateByBo(WellLostRecordBo bo) {
        WellLostRecord update = BeanUtil.toBean(bo, WellLostRecord.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(WellLostRecord add) {
        //TODO 做一些数据校验,如唯一约束
        WellLostRecordVo recordVo =
            baseMapper.selectVoOne(Wrappers.<WellLostRecord>lambdaQuery().eq(WellLostRecord::getComplexType,
                add.getComplexType()).orderByDesc(WellLostRecord::getNum).last("limit 1"));
        int num = 1;
        if (ObjectUtil.isNotEmpty(recordVo) && ObjectUtil.isNotEmpty(recordVo.getNum())) {
            num = recordVo.getNum() + 1;
        }
        add.setNum(num);
        add.setCreateDept(LoginHelper.getDeptId() + "");
    }

    /**
     * 批量删除井漏记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
